var Util = require('../../utils/util.js');
const Layer = require('../layer/layer.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_list:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    that.setData({
      type_id: e.type_id
    })
    wx.getStorage({
      key: 'token',
      success: function (res) {
        if (res.data) {
          that.setData({
            token: res.data
          });
        }
        that.getAddressList();
      }
    });
  },
  getAddressList:function(){
    var that = this;
    var token = that.data.token;
    Util.getAddressList({
      token: token
    }, function (re) {
      console.info(re);
      if (re.code == 200) {
        that.setData({
          address_list: re.data
        })
      }
    })
  },
  setAddAddress: function () {
    wx.navigateTo({
      url: 'addr_add'
    })
  },
  selectAddress: function (e) {
    var addr_id = e.currentTarget.dataset.id;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    var address_list = this.data.address_list;
    var addr_list = prevPage.data.addr_list;
    var addr_num = addr_list.length;
    var check = true;
    for (var i = 0; i < addr_num;i++){
      if (addr_list[i].id==address_list[addr_id].id){
        check = false;
      }
    }
    if(check){
      if (addr_num>0){
        addr_list[addr_num] = address_list[addr_id];
      }else{
        var addr_list = new Array();
        addr_list[0] = address_list[addr_id];
      }
      prevPage.setData({
        addr_list: addr_list,
        addr_num : addr_list.length
      })
      wx.navigateBack({
        delta: 1 
      })  
    }else{
      wx.showToast({
        title: '请选择其他入住人',
        icon: 'none',
        duration: 2000
      })
    } 
  },
  defaultAddress: function (e) {
    var that = this;
    var token = that.data.token;
    var addr_id = e.currentTarget.dataset.id;
    Util.setDefaultAddress({
      token: token,
      addr_id: addr_id
    }, function (re) {
      if (re.code == 200) {
        that.getAddressList();
      }
    })
  },
  editAddress: function (e) {
    var that = this;
    var addr_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'addr_edit?addr_id=' + addr_id
    })
  },
  delAddress: function (e) {
    var that = this;
    var addr_id = e.currentTarget.dataset.id;
    var token = that.data.token;
    Layer.layer.confirm('您确定要删除吗？', ['取消', '确定'], that, '-', function () {
      Layer.layer.close(that);
      Util.delAddress({
        token: token,
        addr_id: addr_id
      }, function (re) {
        if (re.code == 200) {
          that.getAddressList();
        }
      })
    }, function () {

    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        if (res.data) {
          that.setData({
            token: res.data
          });
        }
        that.getAddressList();
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})